package com.smartj.web.common.result.aop;


import com.smartj.web.common.result.ResultUtil;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * 处理ResultAble 切面
 */
@Aspect
@Component
public class ResultInfoAop {

    private final Logger log = LoggerFactory.getLogger(this.getClass());

    /**
     * 切面位置是{@link com.smartj.web.common.result.annotation.ResultAble} 标注的方法
     */
    @Pointcut("@annotation(com.smartj.web.common.result.annotation.ResultAble)")
    public void pointcut() {
    }


    /**
     * 处理切面<P>
     * 如果没有异常则正常返回，如果有异常则将异常包装成Result
     *
     * @param point 连接点
     * @return Result对象
     * @throws Throwable 异常
     */
    @Around("pointcut()")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        Object proceed;
        try {
            proceed = point.proceed();
        } catch (Exception e) {
            log.error(point.getSignature().getName() + ":", e);
            proceed = ResultUtil.error(e);
        }
        return proceed;
    }
}